package com.goods.business.converter;

import com.goods.business.mapper.SupplierMapper;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.InStockVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@Component
public class InStorkConverter {

  @Autowired
  private SupplierMapper supplierMapper;

  /**
   * 转VOList
   * @param inStocks
   * @return
   */
  public List<InStockVO> converterToVOList(List<InStock> inStocks){
    List<InStockVO> inStockVOS = new ArrayList<>();
    if (!CollectionUtils.isEmpty(inStocks)){
      for (InStock inStock : inStocks) {
        InStockVO inStockVO = new InStockVO();
        BeanUtils.copyProperties(inStock,inStockVO);
        Supplier supplier = supplierMapper.selectByPrimaryKey(inStock.getSupplierId());
        if (supplier!=null){
          inStockVO.setSupplierName(supplier.getName());
          inStockVO.setPhone(supplier.getPhone());
        }
        inStockVOS.add(inStockVO);
      }
    }
    return inStockVOS;
  }
}
